package public

import (
	"gin/app/http/global"
	"gin/app/http/model/user"
	"gin/app/http/response"
	"github.com/gin-gonic/gin"
	"time"
)

type LoginData struct {
	// binding:"required"修饰的字段，若接收为空值，则报错，是必须字段
	Username string `form:"username" json:"username"  xml:"username" binding:"required"`
	Password string `form:"password" json:"password"  xml:"password" binding:"required"`
}

func Register(c *gin.Context) {
	var json LoginData
	if err := c.ShouldBindJSON(&json); err != nil {
		response.Fail(c, "注册失败！", gin.H{})
	}
	//util.DB.AutoMigrate(&user.UserModel{})
	//return
	result := global.DB.Create(&user.UserModel{Password: json.Password, UserName: json.Username, CreateAt: time.Now(), UpdatedAt: time.Now()})

	if result.RowsAffected == 1 {
		response.Success(c, "注册成功！", gin.H{})
	} else {
		response.Fail(c, "注册失败", gin.H{"error": result.Error})
	}

}
